home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CD Fun House 1
/
CD Fun House (Wayzata Technology).iso
/
•Word Games•
/
WordFind •••
/
Source
/
sort.p
< prev
next >
Wrap
Text File
|
1987-11-14
|
2KB
|
114 lines
UNIT sorts;
INTERFACE
USES
stringf;
CONST
MAXWORDS = 50;
TYPE
wordlist = ARRAY[1..50] OF STRING;
VAR
ourlist : wordlist;
PROCEDURE ssort1 (n : integer); {by size}
PROCEDURE ssort2 (n : integer); {by alpha}
IMPLEMENTATION
FUNCTION comp1 (i, j : integer) : integer;(* compares length*)
{returns -1 if i > j; 1 if j > i ; and 0 if i = j}
BEGIN
IF length(ourlist[i]) > length(ourlist[j]) THEN
comp1 := -1
ELSE IF length(ourlist[i]) < length(ourlist[j]) THEN
comp1 := 1
ELSE
comp1 := 0;
END;
FUNCTION comp2 (i, j : integer) : integer;
BEGIN
IF strcmp(ourlist[i], ourlist[j]) > 0 THEN
comp2 := 1
ELSE IF strcmp(ourlist[i], ourlist[j]) < 0 THEN
comp2 := -1
ELSE
comp2 := 0;
END;
PROCEDURE swap (i, j : integer);
VAR
s : STRING;
BEGIN
s := ourlist[i];
ourlist[i] := ourlist[j];
ourlist[j] := s;
END;
{This sort is a port from the C programmer's library by}
{Purdum, Leslie and Stegmoller}
PROCEDURE ssort1;
LABEL
100;
VAR
h, i, j, k, m : integer;
BEGIN
m := n;
WHILE (m DIV 2) > 0 DO
BEGIN
m := m DIV 2;
k := n - m;
j := 1;
REPEAT
BEGIN
i := j;
REPEAT
BEGIN
h := i + m;
IF comp1(i, h) > 0 THEN
BEGIN
swap(i, h);
i := i - m;
END
ELSE
GOTO 100;
END;
UNTIL i < 1;
100 :
j := j + 1;
END;
UNTIL j > k;
END;
END;
PROCEDURE ssort2;
LABEL
100;
VAR
h, i, j, k, m : integer;
BEGIN
m := n;
WHILE (m DIV 2) > 0 DO
BEGIN
m := m DIV 2;
k := n - m;
j := 1;
REPEAT
BEGIN
i := j;
REPEAT
BEGIN
h := i + m;
IF comp2(i, h) > 0 THEN
BEGIN
swap(i, h);
i := i - m;
END
ELSE
GOTO 100;
END;
UNTIL i < 1;
100 :
j := j + 1;
END;
UNTIL j > k;
END;
END;
END.